<?php
namespace BoxPay\Hooks;
class OrderHooks extends \HooksController
{
    /****生成订单之前**/
    function order_before_make(&$param)
    {
        $order_amount = $param['order_amount'];
        $this->order_amount_dec($param);
    }

    //优惠金额
    function order_amount_dec(&$param)
    {
					//dump($param);
        $order_amount = $param['order_amount'];
        $card_nums = $param['card_nums'];
        $param['card_nums'] = array();
        /******使用卡券，多个使用逗号分开***/
        if ($card_nums && $order_amount > 0) {
            $param['card_num_money'] = 0;
            foreach ($card_nums as $card_num) {
                $data = Logic("BoxMall", "Card")->check_card($card_num);
                Logic("BoxMall", "Card")->use_card($param['user_id'], $card_num);
                $money = $data['money'];
                if ($money && $order_amount > 0) {
                    $order_amount -= $money;
                    $param['card_nums'][] = $card_num;
                    $param['card_num_money'] += $money;
                } else {
                    $order_amount = 0;
                    break;
                }
            }
        }
        /****使用余额***/
        $user_info = BoxModel("system_member")->where(array('user_id' => $param['user_id']))->find();
								if ($param['use_yie'] && $order_amount > 0) {
												if($param['use_yie']> $user_info['money']){
														$param['use_yie']=$user_info['money'];
													}
													if($param['use_yie']> $order_amount){
														 $param['use_yie']=$order_amount;
													} 
            $order_amount -= $param['use_yie'];
            /******使用课程学币***/
            $num = $param['use_yie'];
            $row = array(
                'user_id' => $param['user_id'],
                'num' => -$num,
                'title' => $param['log_title'],
                'order_sn' => $param['order_sn'],
                'subjectid' => $param['subjectid'],
																'url'=>$param['url'],
																'event'=>$param['event'],
            );
										
            if ($num > 0) {
                $r = Logic('BoxSns', 'Log')->add($row);
            }
        }

        $param['order_amount'] = $order_amount;
        hook("order_amount_dec_" . $param['order_type'], $param);
    }


}
